<?php
/**
 * 文章管理 控制器
 */

namespace App\Http\Controllers\Admin\Article;

use App\Exceptions\ValidatorException;
use App\Http\Controllers\Controller;
use App\Http\Repositories\Repository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class ArticleController extends Controller
{
    private $repository;
    public function __construct(Repository $repository)
    {
        $this->repository = $repository;
    }

    /**
     * 获取
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function getArticleList(Request $request)
    {
        //验证规则
        $rules = [
            'title' => 'nullable|string',
            'article_type' => 'nullable|int',
            'create_time' => 'nullable|array'
         ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->getArticleList($validator->getData());
    }

    /**
     * 详情
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function getArticleInfo(Request $request)
    {
        //验证规则
        $rules = [
            'id' => 'required|int'
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->getArticleInfo($validator->getData());
    }

    /**
     * 添加
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function addArticle(Request $request)
    {
        //验证规则
        $rules = [
            "article_type" => "required",
            "title" => "required|string",
            "cover" => "required|string",
//            "author" => "required|string",
//            "author_uuid" => "required|string",
            "describe" => "nullable|string",
            "content" => "required|string",
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->addArticle($validator->getData());
    }

    /**
     * 删除
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function delArticle(Request $request)
    {
        //验证规则
        $rules = [
            "ids" => "required"
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->delArticle($validator->getData());
    }

    /**
     * 编辑
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function editArticle(Request $request)
    {
        //验证规则
        $rules = [
            'id' => 'required',
            'title' => 'nullable|string',
            'cover' => 'nullable',
            'content' => 'nullable|string',
            'describe' => 'nullable|string',
            'status' => 'nullable|int'
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->editArticle($validator->getData());
    }

}
